//
//  ThingTableView.m
//  one·一个  tyw
//
//  Created by tyw on 15/9/9.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "ThingTableView.h"
#import "DayNightLabel.h"
#import "ZoomImgView.h"
#import "UIImageView+WebCache.h"

@implementation ThingTableView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        self.backgroundColor = [UIColor clearColor];
        
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

#pragma mark - setDataDic
- (void)setDataDic:(NSDictionary *)dataDic {
    _dataDic = dataDic;
    
    [self reloadData];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [[UITableViewCell alloc] init];
    
    if (indexPath.row == 0) {       //时间
        DayNightLabel* timeLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 0, kWidth-20, 20)];
        [timeLabel loadLabelColor];
        timeLabel.text = [Tools exchangeDate:_dataDic[@"strTm"]];
        timeLabel.textAlignment = NSTextAlignmentLeft;
        timeLabel.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:timeLabel];
    } else if (indexPath.row == 1) {    //图片
        ZoomImgView* imgView = [[ZoomImgView alloc] initWithFrame:CGRectMake(0, 5, kWidth, kHeight/2-10)];
//        imgView.backgroundColor = [UIColor redColor];
//        NSString* imgUrl = _dataDic[@"strBu"];
//        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
//        imgView.image = [UIImage imageWithData:data];
        NSString* imgUrl = _dataDic[@"strBu"];
        if (imgUrl.length > 0) {
            [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        }
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [cell.contentView addSubview:imgView];
    } else if (indexPath.row == 2) {    //标题
        DayNightLabel* titleLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 0, kWidth-100, 50)];
        [titleLabel loadLabelColor];
        titleLabel.text = _dataDic[@"strTt"];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.numberOfLines = 0;
        [cell.contentView addSubview:titleLabel];
    } else {                            //介绍
        NSString* str = _dataDic[@"strTc"];
        CGFloat height = [Tools contentSizeWithMaxWidth:kWidth-20 text:str fontOfSize:12].height;
        DayNightLabel* titleLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(10, 0, kWidth-20, height+10)];
        [titleLabel loadLabelColor];
        titleLabel.text = str;
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.numberOfLines = 0;
        [cell.contentView addSubview:titleLabel];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 20;
    } else if (indexPath.row == 1) {
        return kHeight/2;
    } else if (indexPath.row == 2) {
        return 50;
    } else {
        //自适应
        NSString* str = _dataDic[@"strTc"];
        CGFloat height = [Tools contentSizeWithMaxWidth:kWidth-20 text:str fontOfSize:12].height;
        return height+20;
    }
}


@end
